﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static PuzzleNew.ImageManager;

namespace PuzzleNew
{
    public class ImageManager
    {
        //单元格结构体、图片方向
        private struct Node
        {
            public Image Img;
            public int dir;
        }
        //拼图小图片
        private Image _img;
        //单元格数组
        private Node[,] node;
        //单元格行列数
        public int N = 3;
        //拼图大图边长
        public int Width;
        public int Height;

        public ImageManager(Image Img, Size size)
        {
            this._img = Img;
            this.Width = size.Width;
            this.Height = size.Height;
            node = new Node[3, 3];
            //截取部分图片
            for (int x = 0; x < this.N; x++)
            {
                for (int y = 0; y < this.N; y++)
                {
                    node[x, y].Img = CaptureImage(this._img, Img.Width / this.N, Img.Height / this.N, x * (Img.Width / this.N), y * (Img.Height / this.N));
                    //定义正确方向是1
                    node[x, y].dir = 1;
                }
            }
            //打乱方向
            this.PuzzleTurn();
        }
        /// <summary>
        /// 截取并保存
        /// </summary>
        /// <param name="fromImage">抓取部分</param>
        /// <param name="width">截取宽度</param>
        /// <param name="height">截取高度</param>
        /// <param name="spaceX"></param>
        /// <param name="spaceY"></param>
        /// <returns></returns>
        private Image CaptureImage(Image fromImage, int width, int height, int spaceX, int spaceY)
        {
            //创建新图位图   
            Bitmap bitmap = new Bitmap(width, height);
            //创建作图区域   
            Graphics graphic = Graphics.FromImage(bitmap);
            //截取原图相应区域写入作图区   
            graphic.DrawImage(fromImage, 0, 0, new Rectangle(spaceX, spaceY, width, height), GraphicsUnit.Pixel);
            //保存图片 
            Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
            return saveImage;
        }

        public Image Show(Size size)
        {
            Bitmap bitmap = new Bitmap(this.Width, this.Width);
            //创建作图区域   
            Graphics newGra = Graphics.FromImage(bitmap);
            for (int x = 0; x < this.N; x++)
            {
                for (int y = 0; y < this.N; y++)
                {
                    newGra.DrawImage(node[x, y].Img, new Rectangle(x * Width / N, y * Height / N, Width / N, Height / N));
                    newGra.DrawRectangle(new Pen(Color.White, 3), new Rectangle(x * Width / N, y * Height / N, Width / N, Height / N));
                }
            }
            return bitmap;

        }

        /// <summary>
        /// 随机旋转
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void PuzzleTurn(int x, int y)
        {
            int sum = 1000;
            Random ran = new Random();
            for (int i = 0; i < sum; i++)
            {
                long tick = DateTime.Now.Ticks;
                ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32) | ran.Next());
                switch (ran.Next(1, 4))
                {
                    //旋转不同的角度
                    case 1:
                        node[x, y].Img.RotateFlip(RotateFlipType.RotateNoneFlipNone);
                        break;
                    case 2:
                        node[x, y].Img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                        node[x, y].dir = node[x, y].dir + 1;
                        break;
                    case 3:
                        node[x, y].Img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                        node[x, y].dir = node[x, y].dir + 2;
                        break;
                    case 4:
                        node[x, y].Img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                        node[x, y].dir = node[x, y].dir + 3;
                        break;
                }
                //大于4的时候的循环
                if (node[x, y].dir > 4)
                {
                    node[x, y].dir = node[x, y].dir - 4;
                }
            }

        }
        public bool Rotate(int x, int y)
        {
            node[x, y].Img.RotateFlip(RotateFlipType.Rotate90FlipNone);
            node[x, y].dir++;
            if (node[x, y].dir > 4)
                node[x, y].dir = node[x, y].dir - 4;
            return true;
        }

        public bool Judge()
        {
            //原始方向对应1
            for (int x = 0; x < this.N; x++)
            {
                for (int y = 0; y < this.N; y++)
                {
                    if (this.node[x, y].dir != 1)
                        return false;
                }
            }
            return true;
        }


        public void PuzzleTurn()
        {
            for (int x = 0; x < N; x++)
            {
                for (int y = 0; y < N; y++)
                {
                    this.PuzzleTurn(x, y);
                }
            }
        }

    }
}
